﻿using System.Collections.Generic;

namespace NKnife.SerialBox
{
    public class CheckTransportProtocol
    {
        public const int NONE = 1;
        public const int XOR = 2;
        public const int SUM = 3;
        public const int CRC8 = 4;
        public const int CRC16 = 5;

        private readonly byte[] _auchCrcHi =
        {
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40, 0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41,
            0, 0xc1, 0x81, 0x40, 1, 0xc0, 0x80, 0x41, 1, 0xc0, 0x80, 0x41, 0, 0xc1, 0x81, 0x40
        };

        private readonly byte[] _auchCrcLo =
        {
            0, 0xc0, 0xc1, 1, 0xc3, 3, 2, 0xc2, 0xc6, 6, 7, 0xc7, 5, 0xc5, 0xc4, 4,
            0xcc, 12, 13, 0xcd, 15, 0xcf, 0xce, 14, 10, 0xca, 0xcb, 11, 0xc9, 9, 8, 200,
            0xd8, 0x18, 0x19, 0xd9, 0x1b, 0xdb, 0xda, 0x1a, 30, 0xde, 0xdf, 0x1f, 0xdd, 0x1d, 0x1c, 220,
            20, 0xd4, 0xd5, 0x15, 0xd7, 0x17, 0x16, 0xd6, 210, 0x12, 0x13, 0xd3, 0x11, 0xd1, 0xd0, 0x10,
            240, 0x30, 0x31, 0xf1, 0x33, 0xf3, 0xf2, 50, 0x36, 0xf6, 0xf7, 0x37, 0xf5, 0x35, 0x34, 0xf4,
            60, 0xfc, 0xfd, 0x3d, 0xff, 0x3f, 0x3e, 0xfe, 250, 0x3a, 0x3b, 0xfb, 0x39, 0xf9, 0xf8, 0x38,
            40, 0xe8, 0xe9, 0x29, 0xeb, 0x2b, 0x2a, 0xea, 0xee, 0x2e, 0x2f, 0xef, 0x2d, 0xed, 0xec, 0x2c,
            0xe4, 0x24, 0x25, 0xe5, 0x27, 0xe7, 230, 0x26, 0x22, 0xe2, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xe0,
            160, 0x60, 0x61, 0xa1, 0x63, 0xa3, 0xa2, 0x62, 0x66, 0xa6, 0xa7, 0x67, 0xa5, 0x65, 100, 0xa4,
            0x6c, 0xac, 0xad, 0x6d, 0xaf, 0x6f, 110, 0xae, 170, 0x6a, 0x6b, 0xab, 0x69, 0xa9, 0xa8, 0x68,
            120, 0xb8, 0xb9, 0x79, 0xbb, 0x7b, 0x7a, 0xba, 190, 0x7e, 0x7f, 0xbf, 0x7d, 0xbd, 0xbc, 0x7c,
            180, 0x74, 0x75, 0xb5, 0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71, 0x70, 0xb0,
            80, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 150, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
            0x9c, 0x5c, 0x5d, 0x9d, 0x5f, 0x9f, 0x9e, 0x5e, 90, 0x9a, 0x9b, 0x5b, 0x99, 0x59, 0x58, 0x98,
            0x88, 0x48, 0x49, 0x89, 0x4b, 0x8b, 0x8a, 0x4a, 0x4e, 0x8e, 0x8f, 0x4f, 0x8d, 0x4d, 0x4c, 140,
            0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 70, 0x86, 130, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
        };

        private readonly byte[] _crc8Table =
        {
            0, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
            0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 30, 0x5f, 1, 0xe3, 0xbd, 0x3e, 0x60, 130, 220,
            0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 160, 0xe1, 0xbf, 0x5d, 3, 0x80, 0xde, 60, 0x62,
            190, 0xe0, 2, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
            70, 0x18, 250, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 7,
            0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 6, 0x58, 0x19, 0x47, 0xa5, 0xfb, 120, 0x26, 0xc4, 0x9a,
            0x65, 0x3b, 0xd9, 0x87, 4, 90, 0xb8, 230, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
            0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 100, 0x86, 0xd8, 0x5b, 5, 0xe7, 0xb9,
            140, 210, 0x30, 110, 0xed, 0xb3, 0x51, 15, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
            0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 14, 80,
            0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 12, 0x52, 0xb0, 0xee,
            50, 0x6c, 0x8e, 0xd0, 0x53, 13, 0xef, 0xb1, 240, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
            0xca, 0x94, 0x76, 40, 0xab, 0xf5, 0x17, 0x49, 8, 0x56, 180, 0xea, 0x69, 0x37, 0xd5, 0x8b,
            0x57, 9, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 170, 0x48, 0x16,
            0xe9, 0xb7, 0x55, 11, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 20, 0xf6, 0xa8,
            0x74, 0x2a, 200, 150, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 10, 0x54, 0xd7, 0x89, 0x6b, 0x35
        };

        private uint _checksum;

        public List<byte> ByteArray { get; set; } = new List<byte>();
        
        public uint CalculateChecksum(int checkType)
        {
            switch (checkType)
            {
                case 1:
                    _checksum = 0;
                    break;
                case 2:
                    XorChecksum();
                    break;
                case 3:
                    SumChecksum();
                    break;
                case 4:
                    Crc8Checksum();
                    break;
                case 5:
                    Crc16Checksum();
                    break;
            }
            return _checksum;
        }

        private void Crc16Checksum()
        {
            if (ByteArray.Capacity != 0)
            {
                _checksum = 0;
                byte num2 = 0xff;
                byte num3 = 0xff;
                foreach (var t in ByteArray)
                {
                    var index = num3 ^ t;
                    num3 = (byte) (num2 ^ _auchCrcHi[index]);
                    num2 = _auchCrcLo[index];
                }

                _checksum += num3;
                _checksum = _checksum << 8;
                _checksum += num2;
            }
        }

        private void Crc8Checksum()
        {
            if (ByteArray.Capacity != 0)
            {
                _checksum = 0;
                byte num = 0;
                foreach (var t in ByteArray)
                    num = _crc8Table[num ^ t];

                _checksum = num;
            }
        }

        private void SumChecksum()
        {
            if (ByteArray.Capacity != 0)
            {
                _checksum = 0;
                byte num = 0;
                foreach (var t in ByteArray)
                    num += t;

                num = (byte) (0x100 - num % 0x100);
                _checksum = num;
            }
        }

        private void XorChecksum()
        {
            if (ByteArray.Capacity != 0)
            {
                _checksum = 0;
                var num = (byte) (ByteArray[0] ^ ByteArray[1]);
                for (var i = 2; i < ByteArray.Count; i++) 
                    num ^= ByteArray[i];
                _checksum = num;
            }
        }
    }
}